//
//  THBaseEntity.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "THBaseEntity.h"
@implementation THBaseEntity
MJExtensionCodingImplementation

//+ (NSDictionary *)mj_replacedKeyFromPropertyName
//{
//    NSDictionary *dict = @{@"ID":@"id",@"descriptions":@"description",@"content":@"newContent",@"contents":@"content"};
//    return dict;
//}
- (id)mj_newValueFromOldValue:(id)oldValue property:(MJProperty *)property{
    
    
    //过滤后台不合法的数据
    if ([oldValue isKindOfClass:[NSNull class]])
    {
        if (property.type.isNumberType){
            return 0;
        }else if (property.type.isBoolType)
        {
            return 0;
        }
        else if (property.type.typeClass)
        {
            return nil;
        }else if ([property.type.code isEqualToString:@"NSArray"]){
            return @[];
        }else if ([property.type.code isEqualToString:@"NSDictionary"])
        {
            return @{};
        }
        else{
            return @"";//nil
        }
    }
    //把没有的字段处理成空字符串
    if (!oldValue)
    {
        if (property.type.isNumberType)
        {
            return 0;
        }else if (property.type.isBoolType)
        {
            return 0;
        }else if ([property.type.code isEqualToString:@"NSArray"]){
            return @[];
        }else if ([property.type.code isEqualToString:@"NSString"])
        {
            return @"";
        }
        else if ([property.type.code isEqualToString:@"NSDictionary"])
        {
            return @{};
        }else if (property.type.typeClass)
        {
            return nil;
        }
        else
        {
            return @"";
        }
    }
    return oldValue;
}

- (NSString *)description{
    unsigned int count;
    const char *clasName    = object_getClassName(self);
    NSMutableString *string = [NSMutableString stringWithFormat:@"<%s: %p>:[ \n",clasName, self];
    Class clas              = NSClassFromString([NSString stringWithCString:clasName encoding:NSUTF8StringEncoding]);
    Ivar *ivars             = class_copyIvarList(clas, &count);
    
    for (int i = 0; i < count; i++) {
        
        @autoreleasepool {
            
            Ivar       ivar  = ivars[i];
            const char *name = ivar_getName(ivar);
            
            //得到类型
            const char *type  = ivar_getTypeEncoding(ivar);
            NSString *key     = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
            id       value   = [self valueForKey:key];
            //确保BOOL值输出的是YES 或 NO
          if (strcmp(type,@encode(BOOL)) == 0){
              if (value) {
                  value = ([value integerValue] == 1 ? @"YES" : @"NO");
              }else{
                 value  = @"NO";
              }
          }
            [string appendFormat:@"\t%@: %@\n",[self deleteUnderLine:key], value];
        }
    }
    
    [string appendFormat:@"]"];
    free(ivars);
    return string;
}

/// 去掉下划线
- (NSString *)deleteUnderLine:(NSString *)string{
    
    if ([string hasPrefix:@"_"]) {
        return [string substringFromIndex:1];
    }
    return string;
}
 
@end


